/**
* @ClassName: CredentialsAction
* @Description: 
* @author YuYang
* @date 2013-6-5 上午12:53:52
*/
package com.demodis.action;

import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.demodis.domain.Json;
import com.demodis.domain.TblCredentials;
import com.demodis.service.CredentialsServiceI;
import com.demodis.service.impl.CredentialsServiceImpl;
import com.opensymphony.xwork2.ModelDriven;

public class CredentialsAction extends BaseAction implements ModelDriven<TblCredentials> {

	private static final long serialVersionUID = 1L;
	
	TblCredentials cre = new TblCredentials();
	
	private CredentialsServiceI creService = new CredentialsServiceImpl();
	
	String realPath = ServletActionContext.getServletContext().getRealPath("/imgs/vote/credentials");
	
	public void add() {
		Json json = new Json();
		if (creService.save(cre, realPath + File.separator)) {
			json.setSuccess(true);
			json.setMsg("添加成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("添加失败!");
		}

		super.writeJson(json);
	}
	
	public void delete() {
		Json json = new Json();
		if (creService.delete(cre)) {
			json.setSuccess(true);
			json.setMsg("删除成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("删除失败!");
		}

		super.writeJson(json);
	}
	
	public void modify() {
		Json json = new Json();
		if (creService.modify(cre, realPath + File.separator)) {
			json.setSuccess(true);
			json.setMsg("修改成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("修改失败!");
		}

		super.writeJson(json);
	}
	
	public void query() {
		super.writeJson(creService.get(cre));
	}

	@Override
	public TblCredentials getModel() {
		return cre;
	}

}
